package com.turnengine.client.local.unit.bean;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Storage Item.
 */
public class StorageItem implements IStorageItem, Comparable<IStorageItem> {

	/** The parent. */
	private int parent = -1;
	/** The group. */
	private int group = -1;
	/** The child. */
	private int child = -1;
	/** The size. */
	private long size = 0;

	/**
	 * Creates a new Storage Item.
	 */
	public StorageItem() {
	}

	/**
	 * Creates a new Storage Item.
	 * @param parent the parent
	 * @param group the group
	 * @param child the child
	 * @param size the size
	 */
	public StorageItem(int parent, int group, int child, long size) {
		setParent(parent);
		setGroup(group);
		setChild(child);
		setSize(size);
	}

	/**
	 * Creates a new Storage Item.
	 * @param storageItem the storage item
	 */
	public StorageItem(StorageItem storageItem) {
		setParent(storageItem.getParent());
		setGroup(storageItem.getGroup());
		setChild(storageItem.getChild());
		setSize(storageItem.getSize());
	}

	/**
	 * Creates a new Storage Item.
	 * @param iStorageItem the i storage item
	 */
	public StorageItem(IStorageItem iStorageItem) {
		setParent(iStorageItem.getParent());
		setGroup(iStorageItem.getGroup());
		setChild(iStorageItem.getChild());
		setSize(iStorageItem.getSize());
	}

	/**
	 * Returns the parent.
	 * @return the parent.
	 */
	public int getParent() {
		return parent;
	}

	/**
	 * Returns the group.
	 * @return the group.
	 */
	public int getGroup() {
		return group;
	}

	/**
	 * Returns the child.
	 * @return the child.
	 */
	public int getChild() {
		return child;
	}

	/**
	 * Returns the size.
	 * @return the size.
	 */
	public long getSize() {
		return size;
	}

	/**
	 * Sets the parent.
	 * @param parent the parent to set.
	 */
	public void setParent(int parent) {
		this.parent = parent;
	}

	/**
	 * Sets the group.
	 * @param group the group to set.
	 */
	public void setGroup(int group) {
		this.group = group;
	}

	/**
	 * Sets the child.
	 * @param child the child to set.
	 */
	public void setChild(int child) {
		this.child = child;
	}

	/**
	 * Sets the size.
	 * @param size the size to set.
	 */
	public void setSize(long size) {
		this.size = size;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(1129, 37);
		hash.append(parent);
		hash.append(group);
		hash.append(child);
		hash.append(size);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof StorageItem) {
			StorageItem compare = (StorageItem) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.parent, compare.parent);
			equals.append(this.group, compare.group);
			equals.append(this.child, compare.child);
			equals.append(this.size, compare.size);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IStorageItem compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.parent, compare.getParent());
		builder.append(this.group, compare.getGroup());
		builder.append(this.child, compare.getChild());
		builder.append(this.size, compare.getSize());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("parent", parent);
		string.append("group", group);
		string.append("child", child);
		string.append("size", size);
		return string.toString();
	}
}
